<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评分页面 - 生活状态评分系统</title>
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <link href="/static/custom.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container">
            <a class="navbar-brand" href="/">生活状态评分系统</a>
            <div class="navbar-nav">
                <a class="nav-link active" href="/judge">评分页面</a>
                <a class="nav-link" href="/manage">评分项管理</a>
                <a class="nav-link" href="/history">历史评分</a>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <h2>今日评分</h2>
        <p class="text-muted">日期：{{ today.strftime('%Y-%m-%d') }}</p>
        
        <form id="judgeForm">
            {% for item in items %}
            <div class="card mb-3">
                <div class="card-body">
                    <h5 class="card-title">{{ item.item_name }}</h5>
                    {% if item.is_checkbox %}
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" 
                                   name="item_{{ item.id }}" 
                                   id="item_{{ item.id }}"
                                   value="1"
                                   {% if item.id in submitted_data %}
                                   checked disabled
                                   {% endif %}>
                            <label class="form-check-label" for="item_{{ item.id }}">
                                是
                            </label>
                        </div>
                    {% else %}
                        <div class="btn-group" role="group">
                            {% for option, score in zip_items(item.options, item.scores) %}
                            <input type="radio" 
                                   class="btn-check" 
                                   name="item_{{ item.id }}" 
                                   id="item_{{ item.id }}_{{ loop.index }}" 
                                   value="{{ option }}"
                                   {% if item.id in submitted_data and submitted_data[item.id].selected_option == option %}
                                   checked disabled
                                   {% endif %}>
                            <label class="btn btn-outline-primary" for="item_{{ item.id }}_{{ loop.index }}">
                                {{ option }} ({{ score }}分)
                            </label>
                            {% endfor %}
                        </div>
                    {% endif %}
                    {% if item.id in submitted_data %}
                    <div class="mt-2">
                        <small class="text-muted">已提交评分：{{ submitted_data[item.id].selected_option }} ({{ submitted_data[item.id].score }}分)</small>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
            
            <div class="d-grid gap-2">
                <button type="submit" class="btn btn-primary">提交评分</button>
            </div>
        </form>
    </div>

    <script src="/static/bootstrap.bundle.min.js"></script>
    <script src="/static/custom.js"></script>
    <script>
        document.getElementById('judgeForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = [];
            {% for item in items %}
            const item_{{ item.id }} = document.querySelector('input[name="item_{{ item.id }}"]:checked');
            if (item_{{ item.id }} && !item_{{ item.id }}.disabled) {
                formData.push({
                    id: {{ item.id }},
                    selected_option: item_{{ item.id }}.value,
                    score: {% if item.is_checkbox %}10{% else %}parseInt(item_{{ item.id }}.value.split('(')[1]){% endif %}
                });
            }
            {% endfor %}
            
            if (formData.length === 0) {
                alert('请至少选择一个未提交的评分项');
                return;
            }
            
            try {
                const response = await fetch('/submit', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    alert('提交成功');
                    window.location.reload();
                } else {
                    alert('提交失败：' + result.error);
                }
            } catch (error) {
                alert('提交失败：' + error.message);
            }
        });
    </script>
</body>
</html> 